package com.huaxin.hxcloud.fragment;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.huaxin.common.BaseFragment;
import com.huaxin.common.Entity.HxBulletinList;
import com.huaxin.common.tools.HxTransmitRequestHelper;
import com.huaxin.hxcloud.R;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by hebing on 2017/8/25.
 * 公告Fragment
 */

public class HxNoticeListFragment extends BaseFragment {
    @BindView(R.id.recyclerview)
    RecyclerView mRecyclerview;
    private List<HxBulletinList.BulletinListBean> mListBeanList = new ArrayList<>();
    private HxNoticeListAdapter mHxNoticeListAdapter;

    @Override
    protected int getContentViewId() {
        return R.layout.fragment_warning_list;
    }

    @Override
    public int getLoadFrameId() {
        return R.id.content;
    }

    @Override
    protected void initView(View view) {
        ApiRequsetData();
        mRecyclerview.setLayoutManager(new LinearLayoutManager(getContext()));
        mRecyclerview.setHasFixedSize(true);
        mHxNoticeListAdapter = new HxNoticeListAdapter(getContext(), mListBeanList);
        mRecyclerview.setAdapter(mHxNoticeListAdapter);
    }

    private void ApiRequsetData() {
        updateLoadStatus(LOADING);
        Call<HxBulletinList> bulletinListCall = HxTransmitRequestHelper.getRequestApi().bulletinList();
        bulletinListCall.enqueue(new Callback<HxBulletinList>() {
            @Override
            public void onResponse(Call<HxBulletinList> call, Response<HxBulletinList> response) {
                HxBulletinList bulletinList = response.body();
                if (response.isSuccessful() && bulletinList != null) {
                    if (bulletinList.getBulletinList() == null || bulletinList.getBulletinList().size() == 0) {
                        updateLoadStatus(LOAD_NO_DATA, "暂无公告信息");
                        return;
                    }
                    mListBeanList.addAll(bulletinList.getBulletinList());
                    mHxNoticeListAdapter.notifyDataSetChanged();
                    updateLoadStatus(LOAD_SUCCESS);

                } else {
                    updateLoadStatus(LOAD_ERROR, "加载错误");
                }
            }

            @Override
            public void onFailure(Call<HxBulletinList> call, Throwable t) {
                updateLoadStatus(LOAD_ERROR, "加载失败");
            }
        });
    }
}
